<template>
	<view class="order-container">
		
		<!-- 收货地址 -->
		<view class="address-section" @click="selectAddress">
			<view class="address-content">
				<view class="address-main" v-if="selectedAddress">
					<text class="default-tag" v-if="selectedAddress.isDefault">默认</text>
					<text class="address-text">{{ selectedAddress.province }}{{ selectedAddress.city }}{{ selectedAddress.district }}</text>
				</view>
				<view class="address-main" v-else>
					<text class="address-text">请选择收货地址 ></text>
				</view>
				
				<view class="address-detail-row" v-if="selectedAddress">
					<!-- <text class="detail-label">详细地址</text> -->
					<text class="detail-address">{{ selectedAddress.detailAddress }}</text>
					<uni-icons type="right" size="30"></uni-icons>
				</view>
				<view class="address-detail-row" v-else>
					<text class="detail-label">详细地址</text>
					<text class="detail-arrow">></text>
				</view>
				
				<view class="recipient-info" v-if="selectedAddress">
					<text class="recipient-name">{{ selectedAddress.receiverName }}</text>
					<text class="recipient-phone">{{ selectedAddress.receiverPhone }}</text>
				</view>
				<view class="recipient-info" v-else>
					<text class="recipient-placeholder">请选择收货地址</text>
				</view>
			</view>
			
			<!-- 装饰线 -->
			<view class="decorative-line">
				<view class="line-segment red"></view>
				<view class="line-segment blue"></view>
				<view class="line-segment red"></view>
				<view class="line-segment blue"></view>
			</view>
		</view>
		
		<!-- 商品列表 -->
		<view class="goods-section">
			<view class="store-group" v-for="(store, storeIndex) in groupedStores" :key="storeIndex">
				<!-- 店铺信息 -->
				<view class="store-header">
					<text class="store-name">{{ store.name }}</text>
				</view>
				
				<!-- 优惠活动信息 -->
				<view class="promotion-info" v-if="store.promotion">
					<text class="promotion-text">{{ store.promotion }}</text>
				</view>
				
				<!-- 店铺商品列表 -->
				<view class="store-products">
					<view class="product-item" v-for="(item, index) in store.items" :key="item.id">
						<image class="product-image" :src="item.image" mode="aspectFill"></image>
						<view class="product-info">
							<text class="product-name">{{ item.name }}</text>
							<view class="product-details">
								<text class="product-count">x{{ item.count }}</text>
								<text class="product-price">¥{{ (item.lastprice || item.price || 0).toFixed(2) }}</text>
							</view>
						</view>
					</view>
				</view>
				
				<!-- 订单详情 -->
				<view class="order-details">
					<view class="detail-item" @click="showInvoiceModal">
						<text class="detail-label">发票信息</text>
						<view class="detail-value">
							<text class="detail-text">{{ invoiceInfo }}</text>
							<text class="detail-arrow">></text>
						</view>
					</view>
					<view class="detail-item" @click="selectDelivery">
						<text class="detail-label">配送</text>
						<view class="detail-value">
							<text class="detail-text">物流</text>
							<text class="detail-arrow">></text>
						</view>
					</view>
					<view class="detail-item">
						<text class="detail-label">备注信息</text>
						<view class="detail-value">
							<input 
								class="remarks-input" 
								v-model="remarks" 
								placeholder="请输入备注内容"
								@focus="handleRemarksFocus"
								@blur="handleRemarksBlur"
							/>
						</view>
					</view>
				</view>
			</view>
		</view>
		
		<!-- 订单汇总信息 -->
		<view class="order-summary">
			<view class="summary-item">
				<text class="summary-label">商品合计</text>
				<text class="summary-value">¥{{ totalPrice.toFixed(2) }}</text>
			</view>
			<view class="summary-item">
				<text class="summary-label">运费</text>
				<text class="summary-value">包邮</text>
			</view>
			<view class="summary-item" @click="selectCoupon">
				<text class="summary-label">优惠券</text>
				<view class="summary-value">
					<text class="coupon-text" v-if="selectedCoupon">{{ selectedCoupon.name }}</text>
					<text class="coupon-text" v-else-if="availableCoupons.length > 0">{{ availableCoupons.length }}张可用</text>
					<text class="coupon-text" v-else>0张可用</text>
					<text class="summary-arrow">></text>
				</view>
			</view>
			
			<view class="summary-item">
				<text class="summary-label">优惠券优惠</text>
				<text class="summary-value">- ¥{{ couponDiscount.toFixed(2) }}</text>
			</view>
			<view class="summary-item">
				<text class="summary-label">活动优惠</text>
				<text class="summary-value">- ¥{{ activityDiscount.toFixed(2) }}</text>
			</view>
		</view>
		
		<!-- 底部支付栏 -->
		<view class="bottom-bar">
			<view class="price-info">
				<text class="total-price">¥{{ finalPrice.toFixed(2) }}</text>
			</view>
			<view class="payment-btn" @click="submitOrder">
				微信支付
			</view>
		</view>
		
		<!-- 发票信息弹窗 -->
		<view class="invoice-modal" v-if="showInvoice" @click="closeInvoiceModal">
			<view class="modal-content" @click.stop>
				<!-- 弹窗头部 -->
				<view class="modal-header">
					<text class="modal-title">发票类型</text>
					<text class="modal-close" @click="closeInvoiceModal">×</text>
				</view>
				
				<!-- 发票类型选择 -->
				<view class="invoice-section">
					<view class="section-title">发票类型</view>
					<view class="button-group">
						<view class="option-button" :class="{ active: invoiceType === 'electronic' }" @click="selectInvoiceType('electronic')">
							<text class="button-text">电子普通发票</text>
						</view>
						<view class="option-button" :class="{ active: invoiceType === 'vat' }" @click="selectInvoiceType('vat')">
							<text class="button-text">增值税专用发票</text>
						</view>
					</view>
					<text class="section-desc">电子发票由国家税务总局认可，与纸质发票具有同等法律效力</text>
				</view>
				
				<!-- 发票抬头 -->
				<view class="invoice-section">
					<view class="section-title">发票抬头</view>
					<view class="button-group">
						<view class="option-button" :class="{ active: invoiceTitle === 'personal' }" @click="selectInvoiceTitle('personal')">
							<text class="button-text">个人</text>
						</view>
						<view class="option-button" :class="{ active: invoiceTitle === 'company' }" @click="selectInvoiceTitle('company')">
							<text class="button-text">单位</text>
						</view>
					</view>
					<view class="input-section" v-if="invoiceTitle === 'personal'">
						<text class="input-label">个人名称</text>
						<input class="input-field" v-model="personalName" placeholder="请输入个人名称" />
					</view>
					<view class="input-section" v-if="invoiceTitle === 'company'">
						<text class="input-label">单位名称</text>
						<input class="input-field" v-model="companyName" placeholder="请输入单位名称" />
					</view>
				</view>
				
				<!-- 发票信息 -->
				<view class="invoice-section">
					<view class="section-title">发票信息</view>
					<view class="button-group">
						<view class="option-button" :class="{ active: invoiceContent === 'details' }" @click="selectInvoiceContent('details')">
							<text class="button-text">商品明细</text>
						</view>
						<view class="option-button" :class="{ active: invoiceContent === 'category' }" @click="selectInvoiceContent('category')">
							<text class="button-text">商品类别</text>
						</view>
					</view>
				</view>
				
				<!-- 确认按钮 -->
				<view class="confirm-button" @click="confirmInvoice">
					确定
				</view>
			</view>
		</view>
		
		<!-- 优惠券选择弹窗 -->
		<view class="coupon-modal" v-if="showCoupon" @click="closeCouponModal">
			<view class="modal-content" @click.stop>
				<!-- 弹窗头部 -->
				<view class="modal-header">
					<text class="modal-title">选择优惠券</text>
					<text class="modal-close" @click="closeCouponModal">×</text>
				</view>
				
				<!-- 优惠券列表 -->
				<view class="coupon-list-section">
					<view v-if="availableCoupons.length === 0" class="no-coupon">
						<text>暂无可用优惠券</text>
					</view>
					<view v-else class="coupon-list">
						<view 
							class="coupon-item" 
							v-for="(coupon, index) in availableCoupons" 
							:key="coupon.id || index"
							@click="selectCouponFromModal(coupon)"
						>
							<view class="coupon-left">
								<text class="coupon-amount">{{ coupon.couponName || coupon.name || '优惠券' }}</text>
								<text class="coupon-condition">{{ coupon.consumeThreshold > 0 ? `满${coupon.consumeThreshold}可用` : '无门槛' }}</text>
							</view>
							<view class="coupon-right">
								<text class="coupon-store">{{ coupon.storeName || '全店通用' }}</text>
								<text class="coupon-scope">使用范围: 全店商品</text>
								<text class="coupon-status">使用</text>
								<text class="coupon-expire">有效期至: {{ formatCouponDate(coupon.endTime) }}</text>
								<view class="coupon-watermark">优</view>
							</view>
						</view>
					</view>
				</view>
				
				<!-- 底部按钮 -->
				<view class="coupon-actions">
					<view class="action-btn cancel-btn" @click="cancelCouponSelection">
						不使用优惠券
					</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	import { getAddressList } from '@/api/address.js'
	import { getCurrentMemberId } from '@/utils/userState.js'
	import { get, post } from '@/config/request.js'
	
	export default {
		data() {
			return {
				selectedAddress: null, // 选中的收货地址，初始为null
				selectedItems: [], // 从购物车传递过来的商品列表
				totalPrice: 0, // 商品总价
				discount: 0, // 优惠金额
				deliveryFee: 0, // 运费
				remarks: '', // 备注信息
				invoiceInfo: '不开发票', // 发票信息
				deliveryMethod: '物流', // 配送方式
				selectedCoupon: null, // 选中的优惠券
				couponDiscount: 0, // 优惠券折扣
				activityDiscount: 0, // 活动优惠（与优惠券合并计算）
				
				// 发票弹窗相关
				showInvoice: false,
				invoiceType: 'electronic', // 发票类型：electronic-电子普通发票，vat-增值税专用发票
				invoiceTitle: 'personal', // 发票抬头：personal-个人，company-单位
				invoiceContent: 'details', // 发票信息：details-商品明细，category-商品类别
				personalName: '', // 个人名称
				companyName: '', // 单位名称
				
				// 优惠券弹窗相关
				showCoupon: false,
				availableCoupons: [] // 可用优惠券列表
			}
		},
		onLoad(options) {
			console.log('填写订单页面 onLoad，接收到的参数:', options)
			
			// 接收从购物车页面传递的数据
			if (options.items) {
				try {
					this.selectedItems = JSON.parse(decodeURIComponent(options.items))
					console.log('解析后的商品数据:', this.selectedItems)
				} catch (error) {
					console.error('解析商品数据失败:', error)
					uni.showToast({ title: '数据解析失败', icon: 'none' })
				}
			}
			if (options.totalPrice) {
				this.totalPrice = parseFloat(options.totalPrice)
				console.log('总价:', this.totalPrice)
			}
			if (options.discount) {
				this.activityDiscount = parseFloat(options.discount)
				console.log('优惠金额:', this.activityDiscount)
			}
			
			// 监听地址选择事件
			uni.$on('addressSelected', this.handleAddressSelected);
			// 监听优惠券选择事件
			uni.$on('couponSelected', this.handleCouponSelected);
			
			// 获取默认地址
			this.getDefaultAddress();
		},
		
		onShow() {
			// 页面显示时，如果没有选中地址，尝试获取默认地址
			if (!this.selectedAddress) {
				this.getDefaultAddress();
			}
			
			// 如果有商品信息，尝试获取可用优惠券数量
			if (this.selectedItems && this.selectedItems.length > 0 && !this.selectedCoupon) {
				this.getAvailableCouponCount();
			}
		},
		
		onUnload() {
			// 移除事件监听
			uni.$off('addressSelected', this.handleAddressSelected);
			uni.$off('couponSelected', this.handleCouponSelected);
		},
		computed: {
			// 按店铺分组商品
			groupedStores() {
				const stores = {}
				this.selectedItems.forEach(item => {
					if (!stores[item.storeName]) {
						stores[item.storeName] = {
							name: item.storeName,
							items: [],
							promotion: item.promotion
						}
					}
					stores[item.storeName].items.push(item)
				})
				return Object.values(stores)
			},
			// 计算最终价格
			finalPrice() {
				return this.totalPrice - this.activityDiscount - this.couponDiscount
			}
		},
		methods: {
			// 返回上一页
			goBack() {
				uni.navigateBack()
			},
			
			// 选择收货地址
			selectAddress() {
				uni.navigateTo({
					url: '/pages/dizhiguanli/dizhiguanli'
				});
			},
			
			// 显示发票弹窗
			showInvoiceModal() {
				this.showInvoice = true;
			},
			
			// 关闭发票弹窗
			closeInvoiceModal() {
				this.showInvoice = false;
			},
			
			// 选择发票类型
			selectInvoiceType(type) {
				this.invoiceType = type;
			},
			
			// 选择发票抬头
			selectInvoiceTitle(title) {
				this.invoiceTitle = title;
			},
			
			// 选择发票信息
			selectInvoiceContent(content) {
				this.invoiceContent = content;
			},
			
			// 确认发票设置
			confirmInvoice() {
				// 根据选择更新发票信息显示
				if (this.invoiceType === 'electronic') {
					if (this.invoiceTitle === 'personal') {
						this.invoiceInfo = this.personalName || '个人电子发票';
					} else {
						this.invoiceInfo = this.companyName || '单位电子发票';
					}
				} else {
					if (this.invoiceTitle === 'personal') {
						this.invoiceInfo = this.personalName || '个人增值税发票';
					} else {
						this.invoiceInfo = this.companyName || '单位增值税发票';
					}
				}
				
				this.closeInvoiceModal();
			},
			
			// 选择配送方式
			selectDelivery() {
				uni.showToast({
					title: '配送方式选择功能开发中',
					icon: 'none'
				});
			},
			
			// 备注输入框获得焦点
			handleRemarksFocus() {
				// 可以在这里添加获得焦点时的处理逻辑
			},
			
			// 备注输入框失去焦点
			handleRemarksBlur() {
				// 可以在这里添加失去焦点时的处理逻辑
			},
			
			// 选择优惠券
			async selectCoupon() {
				try {
					// 获取当前用户ID
					const currentMemberId = getCurrentMemberId();
					if (!currentMemberId) {
						uni.showToast({
							title: '请先登录',
							icon: 'none'
						});
						return;
					}
					
					// 获取商品名称（从第一个商品获取）
					if (!this.selectedItems || this.selectedItems.length === 0) {
						uni.showToast({
							title: '没有商品信息',
							icon: 'none'
						});
						return;
					}
					
					const productName = this.selectedItems[0].name;
					console.log('查询优惠券，商品名称:', productName);
					
					// 调用接口查询商品信息，获取shop_id
					const productRes = await get('/shangpin/platform/list', {
						productName: productName
					});
					
					console.log('商品查询结果:', productRes);
					
					if (productRes && productRes.code === 200 && productRes.rows && productRes.rows.length > 0) {
						const product = productRes.rows[0];
						const shopId = product.shop_id || product.shopId || 1;
						
						console.log('获取到shopId:', shopId);
						
						// 根据shopId和memberId查询优惠券
						const couponRes = await get('/promotion1/userjuan/list', {
							shopId: shopId,
							member_user_id: currentMemberId,
							status: 0
						});
						
						console.log('优惠券查询结果:', couponRes);
						
						if (couponRes && couponRes.code === 200 && couponRes.rows) {
							// 显示优惠券选择弹窗
							this.showCouponModal(couponRes.rows);
						} else {
							uni.showToast({
								title: '暂无可用优惠券',
								icon: 'none'
							});
						}
					} else {
						uni.showToast({
							title: '商品信息查询失败',
							icon: 'none'
						});
					}
				} catch (error) {
					console.error('查询优惠券失败:', error);
					uni.showToast({
						title: '查询优惠券失败',
						icon: 'none'
					});
				}
			},
			
			// 提交订单
			async submitOrder() {
				// 验证是否选择了收货地址
				if (!this.selectedAddress) {
					uni.showToast({
						title: '请选择收货地址',
						icon: 'none'
					});
					return;
				}
				
				// 验证地址信息是否完整
				if (!this.selectedAddress.receiverName || !this.selectedAddress.receiverPhone || 
					!this.selectedAddress.province || !this.selectedAddress.city || 
					!this.selectedAddress.district || !this.selectedAddress.detailAddress) {
					uni.showToast({
						title: '收货地址信息不完整',
						icon: 'none'
					});
					return;
				}
				
				// 验证商品信息
				if (!this.selectedItems || this.selectedItems.length === 0) {
					uni.showToast({
						title: '请选择要购买的商品',
						icon: 'none'
					});
					return;
				}
				
				uni.showLoading({
					title: '正在提交订单...'
				});
				
				try {
					// 获取当前用户ID
					const currentMemberId = getCurrentMemberId();
					if (!currentMemberId) {
						uni.hideLoading();
						uni.showToast({
							title: '请先登录',
							icon: 'none'
						});
						return;
					}
					
					// 构建订单数据，符合后端OrderMain实体结构
					const orderData = {
						memberId: currentMemberId,
						buyerName: '用户', // 可以从用户信息获取
						buyerPhone: this.selectedAddress.receiverPhone,
						orderSource: '小程序',
						orderType: '普通订单',
						orderAmount: this.finalPrice,
						orderStatus: '待支付',
						consigneeName: this.selectedAddress.receiverName,
						consigneePhone: this.selectedAddress.receiverPhone,
						consigneeAddress: `${this.selectedAddress.province}${this.selectedAddress.city}${this.selectedAddress.district}${this.selectedAddress.detailAddress}`,
						paymentMethod: '微信支付',
						buyerNote: this.remarks,
						deliveryMethod: this.deliveryMethod,
						invoiceInfo: this.invoiceInfo,
						// 订单商品列表
						orderProductList: this.selectedItems.map(item => ({
							shopId: item.shopId || 1,
							shopName: item.storeName || '默认店铺',
							productId: item.id,
							productCode: item.productCode || item.id.toString(),
							productName: item.name,
							productImage: item.image,
							color: item.color || '',
							spec: item.spec || '',
							discount: this.activityDiscount,
							unitPrice: item.lastprice || item.price || 0,
							quantity: item.count,
							refundQuantity: 0,
							subtotal: (item.lastprice || item.price || 0) * item.count
						}))
					};
					
					console.log('准备提交的订单数据:', orderData);
					
					// 调用后端API创建订单
					const orderResponse = await createOrder(orderData);
					
					if (orderResponse && orderResponse.code === 200) {
						uni.hideLoading();
						
						// 创建支付记录
						const paymentData = {
							orderId: orderResponse.data.orderId || orderResponse.data,
							paymentMethod: '微信支付',
							paymentAmount: this.finalPrice,
							clientSource: '小程序',
							paymentStatus: '待支付',
							shopName: this.selectedItems[0]?.storeName || '默认店铺'
						};
						
						try {
							await createPaymentRecord(paymentData);
						} catch (paymentError) {
							console.error('创建支付记录失败:', paymentError);
						}
						
						uni.showToast({
							title: '订单创建成功',
							icon: 'success'
						});
						
						// 跳转到支付页面
						setTimeout(() => {
							uni.navigateTo({
								url: `/pages/zhifudingdan/zhifudingdan?orderId=${orderResponse.data.orderId || orderResponse.data}&amount=${this.finalPrice}`
							});
						}, 1500);
					} else {
						throw new Error(orderResponse?.msg || '订单创建失败');
					}
				} catch (error) {
					uni.hideLoading();
					console.error('提交订单失败:', error);
					
					uni.showToast({
						title: error.message || '订单提交失败，请重试',
						icon: 'none'
					});
				}
			},
			
			// 处理地址选择
			handleAddressSelected(address) {
				console.log('收到选中的地址:', address);
				
				// 处理地址数据，确保字段映射正确
				this.selectedAddress = {
					id: address.id,
					receiverName: address.receiverName || address.name || '收货人',
					receiverPhone: address.receiverPhone || address.phone || '',
					province: address.province || '',
					city: address.city || '',
					district: address.district || '',
					detailAddress: address.detailAddress || address.address || '',
					isDefault: address.isDefault === 1,
					addressAlias: address.addressAlias || '',
					memberId: address.memberId
				};
				
				console.log('处理后的地址数据:', this.selectedAddress);
				
				uni.showToast({
					title: '地址选择成功',
					icon: 'success'
				});
			},
			
			// 处理优惠券选择
			handleCouponSelected(coupon) {
				if (coupon) {
					// 更新选中的优惠券
					this.selectedCoupon = {
						id: coupon.id,
						name: coupon.title,
						value: coupon.amount,
						condition: coupon.condition
					};
					this.couponDiscount = coupon.amount;
				} else {
					// 取消优惠券选择
					this.selectedCoupon = null;
					this.couponDiscount = 0;
				}
			},
			
			// 显示优惠券选择弹窗
			showCouponModal(coupons) {
				this.availableCoupons = coupons;
				this.showCoupon = true;
			},
			
			// 关闭优惠券弹窗
			closeCouponModal() {
				this.showCoupon = false;
				this.availableCoupons = [];
			},
			
			// 选择优惠券
			selectCouponFromModal(coupon) {
				console.log('选中的优惠券数据:', coupon);
				
				// 使用 coupon.amount 作为优惠券折扣金额
				const discountAmount = coupon.amount || 0;
				console.log('优惠券折扣金额:', discountAmount);
				
				this.selectedCoupon = {
					id: coupon.id,
					name: coupon.couponName || coupon.name || '优惠券',
					value: discountAmount,
					condition: coupon.consumeThreshold || coupon.condition || 0
				};
				
				// 设置优惠券折扣金额
				this.couponDiscount = discountAmount;
				
				console.log('设置后的优惠券信息:', this.selectedCoupon);
				console.log('设置后的优惠券折扣:', this.couponDiscount);
				
				this.closeCouponModal();
				
				uni.showToast({
					title: '优惠券选择成功',
					icon: 'success'
				});
			},
			
			// 取消优惠券选择
			cancelCouponSelection() {
				this.selectedCoupon = null;
				this.couponDiscount = 0;
				this.closeCouponModal();
				
				uni.showToast({
					title: '已取消优惠券',
					icon: 'none'
				});
			},
			
			// 格式化优惠券日期
			formatCouponDate(dateString) {
				if (!dateString) return '暂无';
				try {
					const date = new Date(dateString);
					return `${date.getFullYear()}-${String(date.getMonth() + 1).padStart(2, '0')}-${String(date.getDate()).padStart(2, '0')}`;
				} catch (error) {
					return '日期格式错误';
				}
			},
			
			// 获取可用优惠券数量（不显示弹窗，只更新数量）
			async getAvailableCouponCount() {
				try {
					const currentMemberId = getCurrentMemberId();
					if (!currentMemberId || !this.selectedItems || this.selectedItems.length === 0) {
						return;
					}
					
					const productName = this.selectedItems[0].name;
					
					// 调用接口查询商品信息，获取shop_id
					const productRes = await get('/shangpin/platform/list', {
						productName: productName
					});
					
					if (productRes && productRes.code === 200 && productRes.rows && productRes.rows.length > 0) {
						const product = productRes.rows[0];
						const shopId = product.shop_id || product.shopId || 1;
						
						// 根据shopId和memberId查询优惠券
						const couponRes = await get('/promotion1/userjuan/list', {
							shopId: shopId,
							member_user_id: currentMemberId,
							status: 0
						});
						
						if (couponRes && couponRes.code === 200 && couponRes.rows) {
							// 更新可用优惠券列表，但不显示弹窗
							this.availableCoupons = couponRes.rows;
						}
					}
				} catch (error) {
					console.error('获取优惠券数量失败:', error);
				}
			},
			
			// 获取默认地址
			async getDefaultAddress() {
				try {
					console.log('开始获取默认地址...');
					const response = await getAddressList();
					
					if (response && response.code === 200) {
						const allAddresses = response.rows || [];
						console.log('获取到的地址列表:', allAddresses);
						
						// 获取当前用户的memberId
						const currentMemberId = getCurrentMemberId();
						
						// 只获取当前用户的地址
						let addressList = allAddresses;
						if (currentMemberId) {
							addressList = allAddresses.filter(addr => addr.memberId === currentMemberId);
							console.log('当前用户memberId:', currentMemberId);
							console.log('过滤后的地址列表:', addressList);
						}
						
						// 查找默认地址
						const defaultAddress = addressList.find(addr => addr.isDefault === 1);
						
						if (defaultAddress) {
							// 处理地址数据，确保字段映射正确
							this.selectedAddress = {
								id: defaultAddress.id,
								receiverName: defaultAddress.receiverName || '收货人',
								receiverPhone: defaultAddress.receiverPhone || '',
								province: defaultAddress.province || '',
								city: defaultAddress.city || '',
								district: defaultAddress.district || '',
								detailAddress: defaultAddress.detailAddress || '',
								isDefault: defaultAddress.isDefault === 1,
								addressAlias: defaultAddress.addressAlias || '',
								memberId: defaultAddress.memberId
							};
							console.log('获取默认地址成功:', this.selectedAddress);
						} else {
							console.log('没有找到默认地址');
							this.selectedAddress = null;
						}
					} else {
						console.error('获取地址列表失败:', response?.msg);
						this.selectedAddress = null;
					}
				} catch (err) {
					console.error('获取默认地址失败:', err);
					this.selectedAddress = null;
				}
			}
		}
	}
</script>

<style scoped>
	.order-container {
		background-color: #f5f5f5;
		min-height: 100vh;
		padding-bottom: 120rpx;
	}
	
	/* 页面头部 */
	.page-header {
		display: flex;
		align-items: center;
		justify-content: space-between;
		padding: 20rpx 30rpx;
		background-color: #fff;
		border-bottom: 1rpx solid #eee;
	}
	
	.header-left {
		width: 60rpx;
		height: 60rpx;
		display: flex;
		align-items: center;
		justify-content: center;
	}
	
	.back-icon {
		font-size: 36rpx;
		color: #333;
	}
	
	.header-title {
		font-size: 32rpx;
		color: #333;
		font-weight: bold;
	}
	
	.header-right {
		display: flex;
		align-items: center;
		gap: 20rpx;
	}
	
	.header-icon {
		font-size: 24rpx;
		color: #666;
	}
	
	/* 收货地址 */
	.address-section {
		background-color: #fff;
		margin: 20rpx;
		border-radius: 12rpx;
		padding: 30rpx;
		position: relative;
	}
	
	.address-content {
		display: flex;
		flex-direction: column;
		gap: 15rpx;
	}
	
	.address-main {
		display: flex;
		align-items: center;
	}
	
	.default-tag {
		background-color: #ff4444;
		color: #fff;
		font-size: 20rpx;
		padding: 4rpx 12rpx;
		border-radius: 8rpx;
		margin-right: 10rpx;
	}
	
	.address-text {
		font-weight: bold;
		font-size: 32rpx;
		color: #333;
	}
	
	.address-detail-row {
		display: flex;
		align-items: center;
		justify-content: space-between;
	}
	
	.detail-label {
		font-size: 28rpx;
		color: #333;
		font-weight: bold;
	}
	
	.detail-arrow {
		font-size: 24rpx;
		color: #ccc;
	}
	
	.recipient-info {
		display: flex;
		align-items: center;
		gap: 20rpx;
	}
	
	.recipient-name {
		font-size: 24rpx;
		color: #666;
	}
	
	.recipient-phone {
		font-size: 24rpx;
		color: #666;
	}
	
	.recipient-placeholder {
		font-size: 24rpx;
		color: #999;
	}
	
	/* 装饰线 */
	.decorative-line {
		display: flex;
		margin-top: 20rpx;
		height: 4rpx;
	}
	
	.line-segment {
		flex: 1;
		height: 100%;
	}
	
	.line-segment.red {
		background-color: #ff4444;
	}
	
	.line-segment.blue {
		background-color: #007aff;
	}
	
	/* 商品列表 */
	.goods-section {
		margin: 20rpx;
	}
	
	.store-group {
		background-color: #fff;
		border-radius: 12rpx;
		margin-bottom: 20rpx;
		overflow: hidden;
	}
	
	.store-header {
		padding: 20rpx 30rpx;
		border-bottom: 1rpx solid #f0f0f0;
	}
	
	.store-name {
		font-size: 28rpx;
		color: #333;
		font-weight: bold;
	}
	
	.promotion-info {
		background-color: #fff5f5;
		padding: 15rpx 30rpx;
		border-bottom: 1rpx solid #f0f0f0;
	}
	
	.promotion-text {
		font-size: 24rpx;
		color: #666;
	}
	
	.store-products {
		padding: 20rpx 30rpx;
	}
	
	.product-item {
		display: flex;
		align-items: flex-start;
		margin-bottom: 20rpx;
	}
	
	.product-item:last-child {
		margin-bottom: 0;
	}
	
	.product-image {
		width: 120rpx;
		height: 120rpx;
		border-radius: 8rpx;
		margin-right: 20rpx;
		background-color: #f0f0f0;
	}
	
	.product-info {
		flex: 1;
	}
	
	.product-name {
		font-size: 28rpx;
		color: #333;
		line-height: 1.4;
		margin-bottom: 15rpx;
		display: -webkit-box;
		-webkit-box-orient: vertical;
		-webkit-line-clamp: 2;
		overflow: hidden;
	}
	
	.product-details {
		display: flex;
		justify-content: space-between;
		align-items: center;
	}
	
	.product-count {
		font-size: 24rpx;
		color: #ff4444;
	}
	
	.product-price {
		font-size: 32rpx;
		color: #ff4444;
		font-weight: bold;
	}
	
	/* 订单详情 */
	.order-details {
		border-top: 1rpx solid #f0f0f0;
		padding: 20rpx 30rpx;
	}
	
	.detail-item {
		display: flex;
		align-items: center;
		justify-content: space-between;
		padding: 15rpx 0;
		border-bottom: 1rpx solid #f0f0f0;
	}
	
	.detail-item:last-child {
		border-bottom: none;
	}
	
	.detail-label {
		font-size: 28rpx;
		color: #333;
	}
	
	.detail-value {
		display: flex;
		align-items: center;
	}
	
	.detail-text {
		font-size: 28rpx;
		color: #666;
		margin-right: 10rpx;
	}
	
	.detail-text.placeholder {
		color: #999;
	}
	
	.detail-arrow {
		font-size: 24rpx;
		color: #ccc;
	}
	
	.remarks-input {
		flex: 1;
		height: 60rpx;
		border: none;
		outline: none;
		font-size: 28rpx;
		color: #666;
		background-color: transparent;
		text-align: right;
	}
	
	.remarks-input::placeholder {
		color: #999;
	}
	
	/* 订单汇总信息 */
	.order-summary {
		background-color: #fff;
		margin: 20rpx;
		border-radius: 12rpx;
		padding: 30rpx;
	}
	
	.summary-item {
		display: flex;
		align-items: center;
		justify-content: space-between;
		padding: 15rpx 0;
		border-bottom: 1rpx solid #f0f0f0;
	}
	
	.summary-item:last-child {
		border-bottom: none;
	}
	
	.summary-label {
		font-size: 28rpx;
		color: #333;
	}
	
	.summary-value {
		display: flex;
		align-items: center;
		font-size: 28rpx;
		color: #333;
	}
	
	.coupon-text {
		font-size: 28rpx;
		color: #ff4444;
		margin-right: 10rpx;
	}
	
	.summary-arrow {
		font-size: 24rpx;
		color: #ccc;
	}
	
	/* 底部支付栏 */
	.bottom-bar {
		position: fixed;
		bottom: 0;
		left: 0;
		right: 0;
		height: 100rpx;
		background-color: #fff;
		border-top: 1rpx solid #eee;
		display: flex;
		align-items: center;
		justify-content: space-between;
		padding: 0 30rpx;
	}
	
	.price-info {
		display: flex;
		align-items: center;
	}
	
	.total-price {
		font-size: 48rpx;
		color: #ff4444;
		font-weight: bold;
	}
	
	.payment-btn {
		width: 200rpx;
		height: 70rpx;
		background-color: #ff6600;
		color: #fff;
		border-radius: 35rpx;
		display: flex;
		align-items: center;
		justify-content: center;
		font-size: 28rpx;
		font-weight: bold;
	}
	
	/* 发票弹窗样式 */
	.invoice-modal {
		position: fixed;
		top: 0;
		left: 0;
		right: 0;
		bottom: 0;
		background-color: rgba(0, 0, 0, 0.5);
		display: flex;
		align-items: flex-end;
		justify-content: center;
		z-index: 9999;
	}
	
	.modal-content {
		width: 100%;
		background-color: #fff;
		border-radius: 20rpx 20rpx 0 0;
		padding: 40rpx 30rpx;
		max-height: 80vh;
		overflow-y: auto;
	}
	
	.modal-header {
		display: flex;
		align-items: center;
		justify-content: space-between;
		margin-bottom: 40rpx;
		padding-bottom: 20rpx;
		border-bottom: 1rpx solid #f0f0f0;
	}
	
	.modal-title {
		font-size: 36rpx;
		color: #333;
		font-weight: bold;
	}
	
	.modal-close {
		font-size: 48rpx;
		color: #999;
		width: 60rpx;
		height: 60rpx;
		display: flex;
		align-items: center;
		justify-content: center;
	}
	
	.invoice-section {
		margin-bottom: 40rpx;
	}
	
	.section-title {
		font-size: 32rpx;
		color: #333;
		font-weight: bold;
		margin-bottom: 20rpx;
	}
	
	.button-group {
		display: flex;
		gap: 20rpx;
		margin-bottom: 15rpx;
	}
	
	.option-button {
		flex: 1;
		height: 80rpx;
		border: 2rpx solid #ddd;
		border-radius: 8rpx;
		display: flex;
		align-items: center;
		justify-content: center;
		background-color: #fff;
		transition: all 0.3s;
	}
	
	.option-button.active {
		border-color: #ff4444;
		background-color: #fff5f5;
	}
	
	.button-text {
		font-size: 28rpx;
		color: #666;
	}
	
	.option-button.active .button-text {
		color: #ff4444;
		font-weight: bold;
	}
	
	.section-desc {
		font-size: 24rpx;
		color: #999;
		line-height: 1.4;
	}
	
	.input-section {
		margin-top: 20rpx;
	}
	
	.input-label {
		font-size: 28rpx;
		color: #333;
		margin-bottom: 15rpx;
		display: block;
	}
	
	.input-field {
		width: 100%;
		height: 80rpx;
		border: 2rpx solid #ddd;
		border-radius: 8rpx;
		padding: 0 20rpx;
		font-size: 28rpx;
		color: #333;
		background-color: #fff;
	}
	
	.confirm-button {
		width: 100%;
		height: 90rpx;
		background-color: #ff4444;
		color: #fff;
		border-radius: 45rpx;
		display: flex;
		align-items: center;
		justify-content: center;
		font-size: 32rpx;
		font-weight: bold;
		margin-top: 40rpx;
	}
	
	/* 优惠券弹窗样式 */
	.coupon-modal {
		position: fixed;
		top: 0;
		left: 0;
		right: 0;
		bottom: 0;
		background-color: rgba(0, 0, 0, 0.5);
		display: flex;
		align-items: flex-end;
		justify-content: center;
		z-index: 9999;
	}
	
	.coupon-list-section {
		max-height: 60vh;
		overflow-y: auto;
	}
	
	.no-coupon {
		text-align: center;
		padding: 80rpx 40rpx;
		color: #999;
		font-size: 28rpx;
	}
	
	.coupon-list {
		display: flex;
		flex-direction: column;
		gap: 20rpx;
	}
	
	.coupon-item {
		display: flex;
		background-color: #fff;
		border-radius: 20rpx;
		overflow: hidden;
		box-shadow: 0 4rpx 20rpx rgba(0, 0, 0, 0.1);
		position: relative;
		cursor: pointer;
		transition: all 0.3s;
	}
	
	.coupon-item:hover {
		transform: translateY(-2rpx);
		box-shadow: 0 6rpx 25rpx rgba(0, 0, 0, 0.15);
	}
	
	.coupon-left {
		background: linear-gradient(135deg, #FF6B35, #FF8C42);
		color: #fff;
		padding: 35rpx 25rpx;
		display: flex;
		flex-direction: column;
		justify-content: center;
		align-items: center;
		min-width: 180rpx;
		position: relative;
	}
	
	.coupon-left::after {
		content: '';
		position: absolute;
		right: -10rpx;
		top: 50%;
		transform: translateY(-50%);
		width: 20rpx;
		height: 20rpx;
		background-color: #f5f5f5;
		border-radius: 50%;
		box-shadow: 0 0 0 4rpx #f5f5f5;
	}
	
	.coupon-amount {
		color: #fff;
		font-size: 30rpx;
		font-weight: bold;
		margin-bottom: 8rpx;
		text-align: center;
		line-height: 1.2;
	}
	
	.coupon-condition {
		color: #fff;
		font-size: 22rpx;
		opacity: 0.9;
		text-align: center;
	}
	
	.coupon-right {
		flex: 1;
		padding: 25rpx 30rpx;
		display: flex;
		flex-direction: column;
		justify-content: space-between;
		position: relative;
		background-color: #fff;
	}
	
	.coupon-store {
		font-size: 26rpx;
		color: #ff4444;
		margin-bottom: 8rpx;
		font-weight: 500;
	}
	
	.coupon-scope {
		font-size: 22rpx;
		color: #333;
		margin-bottom: 8rpx;
	}
	
	.coupon-status {
		font-size: 24rpx;
		color: #333;
		margin-bottom: 8rpx;
		font-weight: 500;
	}
	
	.coupon-expire {
		font-size: 22rpx;
		color: #666;
	}
	
	.coupon-watermark {
		position: absolute;
		right: 20rpx;
		top: 50%;
		transform: translateY(-50%);
		font-size: 120rpx;
		color: rgba(0, 0, 0, 0.05);
		font-weight: bold;
		z-index: 1;
		pointer-events: none;
	}
	
	.coupon-actions {
		margin-top: 40rpx;
		display: flex;
		gap: 20rpx;
	}
	
	.action-btn {
		flex: 1;
		height: 80rpx;
		border-radius: 40rpx;
		display: flex;
		align-items: center;
		justify-content: center;
		font-size: 28rpx;
		font-weight: bold;
		cursor: pointer;
	}
	
	.cancel-btn {
		background-color: #f5f5f5;
		color: #666;
		border: 2rpx solid #ddd;
	}
</style> 